﻿#include <iostream>

using namespace std;

void printfArray(int *array, int length) {
    for (int i = 0; i < length; ++i) {
        cout << array[i] << " ";
    }
    cout << endl;
}

void insertSort(int *array, int length) {
    for (int i = 1; i < length; ++i) {
        int value = array[i];
        for (int j = 0; j < i; ++j) {
            if (value < array[j]) {
                for (int k = i; k > j; --k) {
                    array[k] = array[k - 1];
                }
                array[j] = value;
                break;
            }
        }
    }
}

int main() {
    int array[] = {2, 4, 6, 7, 3, 2, 5, 6, 7, 2, 2, 4, 2, 1, 4, 1, 6, 7, 9, 0, 8};
    int length = (sizeof(array) / sizeof(int));
    cout << "length = " << length << endl;

    cout << "排序前: ";
    printfArray(array, length);

    insertSort(array, length);

    cout << "排序后: ";
    printfArray(array, length);

    return 0;
}
